package com.operation.service.impl;

import com.operation.beans.Result;
import com.operation.beans.State;
import com.operation.beans.TimeAndPeople;
import com.operation.beans.WorkOrderAllInfo;
import com.operation.dao.HandleInfoMapper;
import com.operation.dao.UserInfoMapper;
import com.operation.dao.WorkOrderMapper;
import com.operation.entity.HandleInfo;
import com.operation.entity.UserInfo;
import com.operation.entity.WorkOrder;
import com.operation.service.OperationService;
import com.operation.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OperationServiceImpl extends State implements OperationService{
    @Autowired
    WorkOrderMapper workOrderMapper;
    @Autowired
    HandleInfoMapper handleInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    @Transactional
    public Result receiveWorkOrder(WorkOrder workOrder) {
        String myState = null;
        //对接收时间，修改处理状态为已接单
        workOrder.setReceiveTime(new Date());
        workOrder.setIsReceive(1);
        //a点
        int i = workOrderMapper.updateReceiveTimeByJobNumber(workOrder);
        //b点
        HandleInfo handleInfo = new HandleInfo();
        handleInfo.setStatus(1);
        handleInfo.setJobnumber(workOrder.getJobnumber());
        int j = handleInfoMapper.updateStatus(handleInfo);
        if (i == 1 && j == 1){
            myState = s9;
        }else if (i == 1 && j == -1){
            myState = s10;
        }else if (i == -1 && j == 1){
            myState = s11;
        }else if (i == -1 && j == -1){
            myState = s12;
        }
        return ResultUtil.success(myState,1);
    }

    @Override
    @Transactional
    public Result throwWorkOrder(HandleInfo handleInfo) {
        String myState = null;
        //更新接收人
        handleInfo.setReceiveOne(handleInfo.getReceiveOne());
        handleInfo.setReceiveTwo(handleInfo.getReceiveTwo());
        handleInfo.setStatus(1);
        //a点
        int i = handleInfoMapper.updateReceiver(handleInfo);
        WorkOrder workOrder = new WorkOrder();
        workOrder.setJobnumber(handleInfo.getJobnumber());
        workOrder.setReceiveTime(new Date());
        workOrder.setIsReceive(0);
        //b点
        int j = workOrderMapper.updateReceiveTimeByJobNumber(workOrder);
        if (i == 1 && j == 1){
            myState = s9;
        }else if (i == 1 && j == -1){
            myState = s10;
        }else if (i == -1 && j == 1){
            myState = s11;
        }else if (i == -1 && j == -1){
            myState = s12;
        }
        return ResultUtil.success(myState,0);
    }

    @Override
    public Result AccomplishTask(WorkOrder workOrder) {
        workOrder.setIsComplete(2);
        workOrder.setCompleteTime(new Date());
//        workOrder.setCommentTime(new Date());
//        workOrder.setCommentContent(workOrder.getCommentContent());
//        workOrder.setCommentStar(workOrder.getCommentStar());
//        workOrder.setIsComplete(1);
        workOrderMapper.updateIsComplete(workOrder);
        return ResultUtil.success(null,1);
    }

    @Override
    public Result IsOngoingTasks(HandleInfo handleInfo) {
        return ResultUtil.success(workOrderMapper.findIsOnGoingWorkOrder(handleInfo),0);
    }

    @Override
    public Result queryMyAllWorkOrder(Integer receiveOne){
        return ResultUtil.success(workOrderMapper.findMyAllWorkOrder(receiveOne),0);
    }

    @Override
    @Transactional
    public Result countWorkOrder(TimeAndPeople timeAndPeople) {
        HashMap map = new HashMap();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR,timeAndPeople.getYear());
        c.set(Calendar.MONTH,timeAndPeople.getMonth()-1);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime())+ " 00:00:00";

        c.add(Calendar.MONTH, 1);
        c.add(Calendar.DATE, -1);
        String last = format.format(c.getTime()) + " 23:59:59";

        String startTime = first;
        String endTime = last;
        map.put("总单数",handleInfoMapper.countAllWorkOrder(timeAndPeople.getReceiveOne()).getReceiveOne());
        map.put("本月单",handleInfoMapper.countMonthWorkOrder(
                startTime,
                endTime,
                timeAndPeople.getReceiveOne()
        ).getReceiveOne());
        Integer isComplete = 1;
        if (handleInfoMapper.countIsCompleteWorkOrder(startTime,endTime,timeAndPeople.getReceiveOne(),isComplete
        )!=null){
            map.put("已完成", handleInfoMapper.countIsCompleteWorkOrder(
                    startTime,
                    endTime,
                    timeAndPeople.getReceiveOne(),
                    isComplete
            ).getReceiveOne());

        }else {
            map.put("已完成", 0);
        }
        isComplete = 0;
        if (handleInfoMapper.countIsCompleteWorkOrder(startTime,endTime,timeAndPeople.getReceiveOne(),isComplete
        )!=null){
            map.put("未完成", handleInfoMapper.countIsCompleteWorkOrder(
                    startTime,
                    endTime,
                    timeAndPeople.getReceiveOne(),
                    isComplete
            ).getReceiveOne());
        }else {
            map.put("未完成", 0);
        }
        int status = 1;
        timeAndPeople.setReceiveTwo(timeAndPeople.getReceiveOne());
        if (handleInfoMapper.countStatusCompleteWorkOrder(startTime,endTime,timeAndPeople.getReceiveTwo(),status
        )!=null){
            map.put("甩单数", handleInfoMapper.countStatusCompleteWorkOrder(
                    startTime,
                    endTime,
                    timeAndPeople.getReceiveTwo(),
                    status
            ).getReceiveOne());
        }else{
            map.put("甩单数", 0);
        }

        return ResultUtil.success(map ,0);
    }

    @Override
    public List<WorkOrderAllInfo> isOnGoingWorkOrder(Integer receiveOne) {
        return workOrderMapper.isOnGoingWorkOrder(receiveOne);
    }
}
